{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python自动生成和读取Word文件\n",
    "\n",
    "需要一个类库：python-docx\n",
    "* 官网：https://python-docx.readthedocs.io/\n",
    "* 安装：pip3 install python-docx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 自动生成一个Word文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from docx import Document"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一个word文档对象\n",
    "document = Document()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 添加大标题、普通段落"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<docx.text.paragraph.Paragraph at 0x1ac175d5a48>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 添加文档标题，数字可以是0~9\n",
    "document.add_heading(\"Python语言简介\", 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<docx.text.paragraph.Paragraph at 0x1ac16844748>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 添加文本段落\n",
    "document.add_paragraph(\"\"\"\n",
    "    Python是一种跨平台的计算机程序设计语言。 \n",
    "    是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 添加一级标题、无序列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<docx.text.paragraph.Paragraph at 0x1ac175fb988>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "document.add_heading(\"Python的应用领域\", level=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# List Bullet是圆点列表，List Number是数字列表\n",
    "areas = [\"Web开发\", \"网络爬虫\", \"数据分析\", \"大数据处理\", \"机器学习/深度学习\"]\n",
    "for area in areas:\n",
    "    document.add_paragraph(area, style='List Bullet')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 添加一级标题、表格数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<docx.text.paragraph.Paragraph at 0x1ac176026c8>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "document.add_heading(\"Python的数据类型\", level=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备表格数据\n",
    "table_rows = [\n",
    "    (\"类型\", \"中文名\", \"解释\"),\n",
    "    (\"str\", \"字符串\", \"不可变字符串\"),\n",
    "    (\"list\", \"列表\", \"可包含多种类型，可变\"),\n",
    "    (\"tuple\", \"元组\", \"可包含多种类型，不可变\"),\n",
    "    (\"dict\", \"字典\", \"可变，由键值对组成\"),\n",
    "    (\"set\", \"结合\", \"无序集合、元素唯一\")\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加table，返回table对象\n",
    "table = document.add_table(rows=len(table_rows), cols=3)\n",
    "\n",
    "# 给table对象设置数据\n",
    "for row_number, (english_name, chinese_name, description) in enumerate(table_rows):\n",
    "    row_cells = table.rows[row_number].cells\n",
    "    row_cells[0].text = english_name\n",
    "    row_cells[1].text = chinese_name\n",
    "    row_cells[2].text = description"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 保存文档"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "document.save('Python语言简介.docx')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 读取Word文档"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构造文档对象，传入文件地址\n",
    "document_new = Document('Python语言简介.docx')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 读取所有普通段落"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Python语言简介\n",
      "# \n",
      "    Python是一种跨平台的计算机程序设计语言。 \n",
      "    是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。\n",
      "\n",
      "# Python的应用领域\n",
      "# Web开发\n",
      "# 网络爬虫\n",
      "# 数据分析\n",
      "# 大数据处理\n",
      "# 机器学习/深度学习\n",
      "# Python的数据类型\n"
     ]
    }
   ],
   "source": [
    "# 读取每一段的内容\n",
    "for paragraph in document.paragraphs:\n",
    "    print(\"#\", paragraph.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 读取表格数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['类型', '中文名', '解释']\n",
      "['str', '字符串', '不可变字符串']\n",
      "['list', '列表', '可包含多种类型，可变']\n",
      "['tuple', '元组', '可包含多种类型，不可变']\n",
      "['dict', '字典', '可变，由键值对组成']\n",
      "['set', '结合', '无序集合、元素唯一']\n"
     ]
    }
   ],
   "source": [
    "# 所有表格\n",
    "tables = document.tables\n",
    "for table in tables:\n",
    "    for row in table.rows:\n",
    "        print([cell.text for cell in row.cells])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
